package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserListLogic) UserList(req *types.UserListReq) (resp *types.UserListResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.UserList(ctx, &usercenter.UserListReq{
		Page: req.Page,
	})
	if err != nil {
		return nil, err
	}
	var userList []types.User
	if len(res.UserList) > 0 {
		for _, user := range res.UserList {
			userList = append(userList, types.User{
				Id:       user.Id,
				UpdateAt: user.UpdateAt,
				CreateAt: user.CreateAt,
				Activity: user.Activity,
				Gender:   user.Gender,
				Country:  user.Country,
				Email:    user.Email,
				OpenId:   user.OpenId,
				Phone:    user.Phone,
				Name:     user.Name,
			})
		}
	}

	resp = new(types.UserListResp)
	resp.UserList = userList
	return
}
